package com.spring.sunflower.dialog

import android.app.Activity
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.text.TextUtils
import android.view.View
import android.widget.ExpandableListView
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.interfaces.SimpleCallback
import com.lxj.xpopup.util.XPopupUtils
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.spring.sunflower.R
import com.spring.sunflower.bean.VideoCommentBean
import com.spring.sunflower.bean.VideoCommentReleaseBean
import com.spring.sunflower.bean.VideoSubCommentReleaseBean
import com.spring.sunflower.main.adapter.VideoCommentListAdapter
import com.spring.sunflower.util.DoubleClickUtils
import com.spring.sunflower.util.LogUtils
import kotlinx.android.synthetic.main.layout_tiktok_controller.view.*

/**
 * Created by Kevin on 2021/11/10<br/>
 *
 * Describe:<br/>
 */
class BottomCommentDialog(var context: Activity) : BottomPopupView(context) {
    private var exListView: ExpandableListView? = null
    private var tvCommentNum: TextView? = null
    private var tvComment: TextView? = null
    private var llComment: LinearLayout? = null
    private var ivClose: ImageView? = null
    private var mAdapter: VideoCommentListAdapter? = null
    private var mData: MutableList<VideoCommentBean.DatasBean> = ArrayList()
    private var mGroupPos = -1
    private var mChildPos = -1
    private var refreshLayout: SmartRefreshLayout? = null
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_bottom_comment
    }

    override fun onCreate() {
        super.onCreate()
        refreshLayout = findViewById(R.id.refreshLayout)
        exListView = findViewById(R.id.exListView)
        tvCommentNum = findViewById(R.id.tvCommentNum)
        tvComment = findViewById(R.id.tvComment)
        llComment = findViewById(R.id.llComment)
        ivClose = findViewById(R.id.ivClose)
        mAdapter = VideoCommentListAdapter(context, mData)
        exListView!!.setAdapter(mAdapter)
        exListView!!.setGroupIndicator(null)
        refreshLayout?.let {
            it.setEnableRefresh(false)
            it.setEnableLoadMore(true)
            it.setOnLoadMoreListener {
                listener?.onCommentLoadmore()
            }

        }
        exListView!!.setOnGroupClickListener(object : ExpandableListView.OnGroupClickListener {
            override fun onGroupClick(
                parent: ExpandableListView?,
                v: View?,
                groupPosition: Int,
                id: Long
            ): Boolean {
                mGroupPos = groupPosition
                val datasBean = mData[groupPosition]
                LogUtils.logD("BottomCommentDialog", "setOnGroupClickListener")
                val dialog = BottomCommentInputDialog(context)
                dialog.setOnReleaseClickListener(object :
                    BottomCommentInputDialog.OnReleaseClickListener {
                    override fun onReleaseClick() {
                        val comment = dialog.comment
                        listener?.onSubReleaseComment(comment, datasBean.commentId)
                    }
                })
                XPopup.Builder(context)
                    .autoOpenSoftInput(true)
                    .autoFocusEditText(true)
                    .setPopupCallback(object : SimpleCallback() {
                        override fun onShow(popupView: BasePopupView?) {
                            super.onShow(popupView)
                        }

                        override fun onDismiss(popupView: BasePopupView?) {
                            super.onDismiss(popupView)
                            if (!dialog.isClickRelease) {
                                val comment = dialog.comment
                                if (!TextUtils.isEmpty(comment)) {
                                    tvComment?.text = comment
                                }
                            }
                        }
                    })
                    .asCustom(dialog)
                    .show()
                return true
            }

        })
        mAdapter?.setOnFavClickListener(object : VideoCommentListAdapter.OnFavClickListener {
            override fun onParentFav(groupPosition: Int) {
                if (DoubleClickUtils.isFastLongClick()) {
                    ToastUtils.showShort("请勿频繁操作")
                    return
                }
                mGroupPos = groupPosition
                val datasBean = mData[groupPosition]
                listener?.onParentCommentFav(groupPosition, datasBean.commentId)
            }

            override fun onChildFav(groupPosition: Int, childPosition: Int) {
                if (DoubleClickUtils.isFastLongClick()) {
                    ToastUtils.showShort("请勿频繁操作")
                    return
                }
                mGroupPos = groupPosition
                mChildPos = childPosition
                val commentId = mData[groupPosition].subCommentList[childPosition].commentId
                listener?.onChildCommentFav(groupPosition, childPosition, commentId)
            }

        })
        ivClose?.setOnClickListener { dismiss() }
        llComment?.setOnClickListener {
            val dialog = BottomCommentInputDialog(context)
            dialog.setOnReleaseClickListener(object :
                BottomCommentInputDialog.OnReleaseClickListener {
                override fun onReleaseClick() {
                    val comment = dialog.comment
                    listener?.onReleaseComment(comment)
                }
            })
            XPopup.Builder(context)
                .autoOpenSoftInput(true)
                .autoFocusEditText(true)
                .setPopupCallback(object : SimpleCallback() {
                    override fun onShow(popupView: BasePopupView?) {
                        super.onShow(popupView)
                    }

                    override fun onDismiss(popupView: BasePopupView?) {
                        super.onDismiss(popupView)
                        if (!dialog.isClickRelease) {
                            val comment = dialog.comment
                            if (!TextUtils.isEmpty(comment)) {
                                tvComment?.text = comment
                            }
                        }
                    }
                })
                .asCustom(dialog)
                .show()
        }

    }

    override fun getMaxHeight(): Int {
        return (XPopupUtils.getScreenHeight(context) * .6f).toInt()
    }

    var listener: OnReleaseCommentListener? = null
    fun setOnReleaseCommentListener(l: OnReleaseCommentListener) {
        listener = l
    }

    interface OnReleaseCommentListener {
        fun onReleaseComment(comment: String)
        fun onSubReleaseComment(comment: String, parentCommentId: String)
        fun onParentCommentFav(groupPosition: Int, commentId: String)
        fun onChildCommentFav(groupPosition: Int, childPosition: Int, commentId: String)
        fun onCommentLoadmore()
    }

    fun updateComment(data: MutableList<VideoCommentBean.DatasBean>) {
//        tvCommentNum?.text = data.size.toString()
        mData.addAll(data)
        mAdapter?.notifyDataSetChanged()
//        mAdapter?.addComment(data)
        val size = mAdapter?.data?.size ?: 0
        if (size > 0) {
            for (i in 0 until size) {
                exListView?.expandGroup(i)
            }
        }
        refreshLayout?.let {
            if (it.isLoading) {
                it.finishLoadMore()
            }
        }
    }

    fun addComment(data: VideoCommentReleaseBean.DatasBean) {
        val bean = VideoCommentBean.DatasBean()
        bean.videoId = data.videoId
        bean.commentId = data.commentId
        bean.rootCommentId = data.rootCommentId
        bean.commentMemberId = data.commentMemberId
        bean.commentMemberType = data.commentMemberType
        bean.likeTotal = data.likeTotal
        bean.isLike = data.isLike
        bean.content = data.content
        bean.createTime = data.createTime
        bean.nickname = data.nickname
        bean.photo = data.photo
        bean.isVip = data.isVip
        val subCommentList: MutableList<VideoCommentBean.DatasBean.SubCommentListBean> = ArrayList()
        bean.subCommentList = subCommentList
        mData.add(bean)
        mAdapter?.notifyDataSetChanged()
//        val toInt = tvCommentNum?.text.toString().toInt()
//        tvCommentNum?.text = "${toInt + 1}"

    }

    fun addSubComment(data: VideoSubCommentReleaseBean.DatasBean) {
        val datasBean = mData[mGroupPos]
        val bean = VideoCommentBean.DatasBean.SubCommentListBean()
        bean.videoId = data.videoId
        bean.commentId = data.commentId
        bean.parentCommentId = data.parentCommentId
        bean.rootCommentId = data.rootCommentId
        bean.commentMemberId = data.commentMemberId
        bean.commentMemberType = data.commentMemberType
        bean.likeTotal = data.likeTotal
        bean.isLike = data.isLike
        bean.content = data.content
        bean.createTime = data.createTime
        bean.nickname = data.nickname
        bean.photo = data.photo
        bean.parentCommentMemberId = data.parentCommentMemberId
        bean.parentNickname = data.parentNickname
        bean.isVip = data.isVip
        datasBean.subCommentList?.add(bean)
        mAdapter?.notifyDataSetChanged()
    }

    fun refreshCommentFav(isChild: Boolean) {
        if (isChild) {
            val subCommentListBean = mData[mGroupPos].subCommentList[mChildPos]
            subCommentListBean.isLike = "1"
            mData[mGroupPos].subCommentList[mChildPos] = subCommentListBean
            mAdapter?.notifyDataSetChanged()

        } else {
            val datasBean = mData[mGroupPos]
            datasBean.isLike = "1";
            mData[mGroupPos] = datasBean
            mAdapter?.notifyDataSetChanged()
        }
    }
}